package cn.kgc.userconsumer.controller;

import cn.kgc.userconsumer.client.UserFeignClient;
import cn.kgc.userconsumer.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author yaozhaobao
 * @Date 2021/1/19 16:26
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    UserFeignClient userFeignClient;

    @Autowired
    KafkaTemplate<String,String> kafkaTemplate;


    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public String getUserById(@PathVariable String id){
            //远程服务调用provider数据库调用获取用户信息

            return "获取到id为"+id+"用户信息";
    }


    @RequestMapping(value = "/p/login",method = RequestMethod.POST)
    public User login(@RequestParam String username,@RequestParam String pwd){
       return  userFeignClient.login(username,pwd);
    }



    @RequestMapping(value = "/p/login2",method = RequestMethod.POST)
    public Map login(@RequestBody User user){
        Map map = new HashMap();
        if(userFeignClient.login2(user)==null){
            map.put("success","false");
            map.put("msg","远程服务调用失败");
            return  map;
        }else{
            user =  userFeignClient.login2(user);
            map.put("success","true");
            map.put("data",user);
        }
         return  map;
    }

    @RequestMapping(value = "/userinfo",method =RequestMethod.POST )
    public void userinfo(){
        for (int i=0;i<1000;i++){
            userFeignClient.userinfo(i);
        }
    }


    @RequestMapping(value = "/sendMsgToKafka")
    public String sendMsgToKafka() {
        for (int i = 0; i < 10; i++) {
            kafkaTemplate.send("userconsumer", "dm", "hello,Kafka!--->" + i);
        }
        return "发送消息到Kafka完毕";
    }








}
